<?php
header("Content-Type: text/html; charset=iso-8859-1");
require_once('include.php');
$_SESSION['navigation'] = 'accueil';
$_SESSION['cat'] = 'infos';
$membre_id = check_cookie();
if(!isset($_GET['from_iphone']) && isset($_SESSION['from_iphone'])) unset($_SESSION['from_iphone']);
entete('VBCLL - Bienvenue');
$proposer = ($membre_id) ? "<span class = 'float_right'><button onclick = 'new_info()'>proposer une info</button></span>" : "";
echo "<div id = 'action'>\n";
if($membre_id){
	if(date('m') <= 3 || date('m') > 8){
		$membre = new membre($membre_id);
		$liste_equipes = $membre->equipes+$membre->responsables+$membre->entraineurs;
		if(count($liste_equipes)){
			$titre = 'La semaine de ton &eacute;quipe ! (publi&eacute; le mercredi)';
			foreach($liste_equipes as $equipe_id => $equipe_nom){
				$lastWeek = strtotime("last Thursday");
				$q = "select texte, nom,prenom,membre_id from tweets inner join membres on tweets.membre_id = membres.id where equipe_id = $equipe_id and publie = 'N' and datetime >= '".date("Y-m-d H:i:s",$lastWeek)."' order by datetime desc";
				$r = ExecRequete($q);
				$texte = ($s = LigneSuivante($r)) ? $s->texte : "";
				$auteur = ($texte) ? substr($s->prenom,0,1).". ".substr($s->nom,0,1)."." : "";
				if($auteur && $s->membre_id == $membre_id) $auteur = "moi";
				$corps .= "<div style = 'float: left; width: 485px;line-height: 20px;'>\n";
				$corps .= "<span class = 'bold' style = 'vertical-align: top'>$equipe_nom:</span>\n";
				$corps .= "<div id = 'tweet_$equipe_id' style = 'display: inline;'>".(($texte)?$texte:"<span class = 'fade'>Ecrire un (bon) mot (publi&eacute; le mercredi)</span>")."</div>";
				$corps .= "<textarea name = 'edit_tweet_$equipe_id' id = 'edit_tweet_$equipe_id' style = 'display: none' cols = '60' rows = '2'>$texte</textarea>\n";
				$corps .= "</div>\n";
				// $corps .= "<span id = 'tweet_auteur' style = 'float: left; font-style: italic;padding-right: 5px;'>".$auteur."</span>\n";
				$button = ($texte) ? "modif." : "&eacute;crire";
				$corps .= "<button type = 'button' id = 'button_tweet_$equipe_id'  onclick = 'insert_tweet($equipe_id)' style = 'float: left'><img src = 'images/icon_edit.png' alt = '' height = '14' width = '14' />$button</button>\n";
				$corps .= "<br class = 'clear' />\n";
			}
			echo "<div id = 'div_info_equipe'>\n";
			echo box($titre,$corps,690,1);
			echo "</div>\n";
		}
	}
}
echo "<h1>Les derni&egrave;res infos$proposer</h1>\n";
echo "<div id = 'infos_box'>\n";
echo "<div id = 'previous'></div>\n";
echo "<div id = 'infos'></div>\n";
echo "<div id = 'next_info'></div>\n";
echo "</div>\n";
echo "<script type = 'text/javascript'>liste_infos(0,'accueil',0)</script>\n";

echo "<h1>Les derniers r&eacute;sultats</h1>\n";

##### Resultats #######
echo "<div id = 'resultats'>\n";
$fields = array("equipe" => '',"local" => '',"visiteur" => '',"date"  => 'center',"resultat" => 'center',"stat" => 'center');
$equipes = liste('equipes');
$matchs = array();
foreach($equipes as $id => $nom){
	${"match_".$id} = new match('last','',$nom);
	if((${"match_".$id}->no)) $matchs[${"match_".$id}->date.${"match_".$id}->no] = ${"match_".$id};
}
$q = "select no,equipe from matchs where resultat like '%-%' and date > date_sub(curdate(), interval 7 day) and date <= curdate()";
$r = ExecRequete($q);
while($s = LigneSuivante($r)){
	$id = $s->equipe;
	${"match_".$id} = new match($s->no);
	if((${"match_".$id}->no)) $matchs[${"match_".$id}->date.${"match_".$id}->no] = ${"match_".$id};

}
krsort($matchs);
$i = 0;
foreach($matchs as $key => $match){
	$equipe = new equipe($match->equipe,"nom");
	if(++$i == 1){
		echo "<table class = 'list'>\n";
		echo "<tr class = 'TH'>\n";
		foreach($fields as $field => $class){
			echo "	<th>$field</th>\n";
		}
		echo "<th>class.</th>\n";
		echo "</tr>\n";
	}
	echo "<tr class = 'A1'>\n";
	$equipe = new equipe($match->equipe,"nom");
	foreach($fields as $field => $class){
		if($field == 'date') $print = format_date($match->$field,'d.m');
		elseif($field == 'equipe') $print = "<a href = 'equipes.php?id=$equipe->id'>$match->equipe</a>";
		elseif($field == 'resultat') $print = $match->format_resultat();
		elseif(($field == 'local' && strstr($match->$field,'LUTRY') === FALSE) || ($field == 'visiteur' && strstr($match->$field,'LUTRY') === FALSE))	$print = "<span class = 'bold'>".$match->$field."</span>\n";
		elseif($field == 'stat') $print = "<a href = 'javascript:stats_match(\"$match->no\")'>".$match->$field."</a>\n";
		elseif($field == 'equipe') $print = "<a href = 'equipes.php?id=$equipe->id'>$match->equipe</a>\n";
		else $print = $match->$field;
		echo "<td class = '$class'>".($print)."</td>\n";
	}

	echo "<td class = 'center' style = 'cursor: pointer'><a href = 'javascript:print_classement(\"$equipe->id\")'>$equipe->classement</a></td>\n";
	echo "</tr>\n";
}
if($i) echo "</table>\n";
else echo "<p class = 'center fade'>Le championnat n'a pas encore d&eacute;but&eacute;</p>\n";
echo "</div>\n";


###### Prochains ########
echo "<h1>Les prochains matchs</h1>\n";
echo "<div id = 'prochains'>\n";

$fields = array("equipe" => '',"local" => '',"visiteur" => '',"jour" => 'center',"date"  => 'center nowrap',"heure" => "center nowrap","salle" => '',"stat" => 'center nowrap');
$equipes = liste('equipes');
$matchs_m1 = array();
$matchs = array();
foreach($equipes as $id => $nom){
	${"match_".$id} = new match('next','',$nom);
	if(preg_match("/^[0-3]-[0-3]$/",${"match_".$id}->resultat) == 0 && (${"match_".$id}->no)) $matchs[${"match_".$id}->date.${"match_".$id}->no] = ${"match_".$id};
	if($nom == 'LNA'||$nom == 'LNB') $matchs_m1[${"match_".$id}->no] = ${"match_".$id}->visiteur;
}
$q = "select no,equipe from matchs where date >= curdate() and date < date_add(curdate(),interval 7 day)";
$r = ExecRequete($q);
while($s = LigneSuivante($r)){
	$id = $s->equipe;
	${"match_".$id} = new match($s->no);
	if(preg_match("/^[0-3]-[0-3]$/",${"match_".$id}->resultat) == 0 && (${"match_".$id}->no)) $matchs[${"match_".$id}->date.${"match_".$id}->no] = ${"match_".$id};
	if($id == 'LNA'||$id == 'LNB') $matchs_m1[${"match_".$id}->no] = ${"match_".$id}->visiteur;
}
// }
ksort($matchs);
$i = 0;


// ballon de match //
if(count($matchs_m1)){
	$ballons = array();
	foreach($matchs_m1 as $no_m1 => $visiteur_m1){
		$q1 = "select concat(partenaires.nom,' ',partenaires.prenom) as partenaire,localite,url from ballon_matchs inner join matchs on ballon_matchs.match_no = matchs.no inner join partenaires on ballon_matchs.partenaire_id = partenaires.id where matchs.no = '$no_m1'";
		$r1 = ExecRequete($q1);
		while($s1 = LigneSuivante($r1)){
			$ballon = new stdClass;
			$ballon->match = $no_m1;
			$ballon->visiteur = $visiteur_m1;
			$ballon->partenaire = $s1->partenaire;
			$ballon->localite = $s1->localite;
			$ballon->url = $s1->url;
			$ballons[$no_m1] = $ballon;
		}
	}
	if(count($ballons)){
		echo "<div id = 'ballons'>\n";
		foreach($ballons as $no_match => $ballon){
			echo "<p>Le ballon du match contre $ballon->visiteur est offert par </p><p style = 'line-height: 13px'><span class = 'bold' style = 'letter-spacing: 1px;'>";
			if($ballon->url) echo "<a href = '$ballon->url' target = '_blank'>";
			echo (strtoupper($ballon->partenaire))."</span>";
			if($ballon->url) echo "</a>";
			echo " à $ballon->localite</p>\n";
		}
		echo "</div>\n";
	}
}

foreach($matchs as $key => $match){
	if(++$i == 1){
		echo "<table class = 'list'>\n";
		echo "<tr class = 'TH'>\n";
		foreach($fields as $field => $class){
			echo "	<th>$field</th>\n";
		}
		echo "</tr>\n";
	}
	echo "<tr class = 'A1'>\n";
	$equipe_id = ($key = array_search($match->equipe,$equipes)) ? $key : "";
	foreach($fields as $field => $class){
		$lien_equipe = ($key = (array_search($match->equipe,$equipes))) ? $key : "";
		if($field == 'date') $print = format_date($match->$field,'d.m');
		elseif($field == 'equipe') $print = "<a href = 'equipes.php?id=".$equipe_id."'>$match->equipe</a>\n";
		elseif($field == 'heure') $print = format_heure($match->$field);
		elseif(($field == 'local' && strstr($match->$field,'LUTRY') === FALSE) || ($field == 'visiteur' && strstr($match->$field,'LUTRY') === FALSE))	$print = "<span class = 'bold'>".$match->$field."</span>\n";
		elseif($field == 'salle'){
			$salle = new salle($match->$field);
			$print = $salle->lien();
		}
		elseif($field == 'stat') $print = "<a href = 'javascript:stats_match(\"$match->no\")'>".$match->$field."</a>\n";
		elseif($field == 'equipe') $print = ($lien_equipe) ? "<a href = 'equipes.php?id=$lien_equipe'>$match->equipe</a>" : $match->equipe;
		else $print = $match->$field;
		echo "<td class = '$class'>".($print)."</td>\n";
	}
	echo "</tr>\n";
}
if($i) echo "</table>\n";
else echo "<p class = 'center fade'>Le championnat est termin&eacute;</p>\n";
echo "</div>\n";
echo "</div>\n";
pied();
?>
